<?php
/**
 * 用户算力矿机订单
 * lvsen
 */
namespace app\api\model;
use app\common\model\MemberArithmeticalPower as MemberArithmeticalPowerModel;
use app\common\model\MiningArithmetical as MiningArithmeticalModel;

class MemberArithmeticalPower extends MemberArithmeticalPowerModel
{

    /**
     * 算力矿机订单
     * @param $mid 用户ID
     * @param array $where 查询条件
     * @param string $order 排序
     * @param string $language 语言
     * @return MemberArithmeticalPower[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getList($mid,$page = 1,$limit = 20,$where = [],$order = 'id desc',$language = ''){
        $data = self::where('mid',$mid);
        if(empty($where)) $data = $data->where($where)->order($order);
        $data = $data->paginate(['list_rows'=>$limit,'page'=>$page]);
        if(!empty($data)) $data->toArray();

        foreach ($data as $k => &$v){
            $arithmetical = MiningArithmeticalModel::getDetails($v['maid'],$language);
            $v['arithmetical'] = !empty($arithmetical) ? $arithmetical->toArray() : [];
        }
        return $data;
    }

    /**
     * 查询用户所有列表
     * @param $mid 用户ID
     * @param $where 查询条件
     * @param string $order 排序
     * @param string $language 语言
     * @return MemberArithmeticalPower[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getMemberAll($mid,$where,$order = 'id desc',$language = ''){
        $data = self::where('mid',$mid);
        if($where) $data = $data->where($where);
        $data = $data->order($order)->select();

        foreach ($data as $k => &$v){
            $v['arithmetical'] = MiningArithmeticalModel::getDetails($v['maid'],$language);
        }

        return $data;
    }

    /**
     * 添加订单
     * @param $mid  用户ID
     * @param $maid 矿机ID
     * @param $info 矿机详情
     * @return int|string
     */
    public static function add($mid,$maid,$info){
        $data = [
            'mid'   =>  $mid,
            'maid'  =>  $maid,
            'money' =>  $info['money'],
            'currency'=>    $info['currency'],
            'pay_status'    =>  2,
            'pay_type'  =>  1,
            'arithmetical_power'    =>  $info['arithmetical'],  //矿机算力
            'surplus_arithmetical'  =>  $info['arithmetical'],  //剩余矿机算力
            'pledge'    =>  1,
            'add_time'  =>  date('Y-m-d H:i:s',time())
        ];
        $id = self::insertGetId($data);
        return $id;
    }

    /**
     * 查询订单
     * @param $mid  用户ID
     * @param $id   订单ID
     * @return MemberArithmeticalPower|array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getMemberDetails($mid,$id)
    {
        $info = self::where('mid',$mid)->where('id',$id)->find();
        return $info;
    }

    /**
     * 修改矿机剩余算力
     * @param $id   矿机订单ID
     * @param $mid  用户ID
     * @param $surplus_arithmetical 矿机剩余算力数量
     */
    public static function up_surplus_arithmetical($id,$mid,$surplus_arithmetical){
        self::where('id',$id)->where('mid',$mid)->update(['surplus_arithmetical'=>$surplus_arithmetical]);
    }

    /**
     * 质押矿机
     * @param $id 矿机ID
     * @param $money 质押金额
     * @return bool
     */
    public static function pledge($id,$money){
        $data = [
            'pledge'    =>  2,
            'pledge_money'  =>  $money,
            'pledge_time'   =>  time(),
            'update_time'   =>  date('Y-m-d H:i:s',time())
        ];
        self::where('id',$id)->update($data);
        return true;
    }

    /**
     * 质押矿机
     * @param $id 矿机ID
     * @param $money 返回质押金额
     * @return bool
     */
    public static function cancel_pledge($id,$money){
        $data = [
            'pledge_money' =>  $money,
            'update_time'   =>  date('Y-m-d H:i:s',time())
        ];
        if($money==0) $data['pledge'] = 3;
        self::where('id',$id)->update($data);
        return true;
    }

}